clear all

//Set CD for analyses
cd "MYCD" ///MAKE SURE TO SET!!!!


//Code variables and create scales
run "MayerSmithPartisanshipCC_CODING.do"


//Install necesarry user-written commands
foreach f in polychoric conjoint coefplot catplot heatplot {
cap which `f'
if _rc {
net install `f'.pkg
}
}

//Set control, DV and IV lists
local controls ///
	clim_con ///
	trust_soc trust_ins trust_sci ///
	female b6.age ///
	b1.racethn b3.region ///
	b2.educ income ///
	rural_urb	

loc iv ///
	party_id ///
	expressive ///
	neg_part
	
loc pol ///
	fftax ///
	renew ///
	hhapp
	
local behav ///
	offsets ///
	commute ///
	meat ///
	bath 
	
	
loc lc_party_id teal
loc lc_expressive sienna
loc lc_neg_part forest_green

//Figure 1 - Correlation and Partisanship Measures
*IV distribution
catplot party_id, ///
	perc  asyvars vert ///
	showyvars yvar(lab(labs(small) angle(45))) ///
	ti("`: var label party_id'") ///
	yti("Percent of Responses", s(small)) ///
	b1title("",) ///
	bar(1, bfcolor(maroon) lc(maroon)) ///
	bar(2, bfcolor(maroon%66) lc(maroon%66)) ///
	bar(3, bfcolor(maroon%33) lc(maroon%33)) ///
	bar(4, bfcolor(sand%75) lc(sand%50)) ///
	bar(5, bfcolor(edkblue%33) lc(edkblue%33)) ///
	bar(6, bfcolor(edkblue%66) lc(edkblue%66)) ///
	bar(7, bfcolor(edkblue) lc(edkblue)) ///
	yline(0, lc(black) lp(solid)) ///
	bargap(0.7) ///
	legend(off) ///
	note("{bf:B)}", pos(10) ring(12) size(small)) ///
	name(party_id, replace) nodraw

	
foreach p in expressive neg_part {
	twoway ///
	(hist `p'_dem, percent lcolor(edkblue%50) fcolor(edkblue%50)) ///
	(hist `p'_gop, percent fcolor(none) lcolor(maroon)), ///
	ti("`: var label `p''", s(large)) ///
	yti("Percent of Responses", s(medsmall)) ///
	xti("") ///
	xlab( ///
		-2 "-2Sd" -1 "-1Sd" ///
		0 "Mean" 1 "+1Sd" ///
		2 "+2Sd", labs(medsmall) nogrid) ///
	legend( ///
		order(1 "Democrat" 2 "Republican") ///
		row(1) pos(6)) ///
	name(`p', replace) nodraw
}
	
gr combine ///
	expressive neg_part, ///
	row(1) ///
	note("{bf:C)}", pos(10) ring(12) size(small)) ///
	name(exp_neg, replace) nodraw
	
gr combine ///
	party_id ///
	exp_neg, ///
	col(1) ///
	ysize(10) ///
	name(iv_dist, replace)
gr close iv_dist

*correlation
pwcorr party_id expressive neg_part female age  ///
	educ income 

matrix C= r(C)
set scheme plotplain
heatplot C, values(format(%3.2f)) ///
	color(hcl, diverging intensity(.6)) ///
	legend(off subtitle("{it:r}")) lower nodiagonal ///
	ylab(,glc(white)) xlab(,glc(white)) label ///
	cuts(-0.55(0.1)0.55) keyl(,f(%2.1f)) ///
	xlab(,angle(45) labs(vsmall)) ///
	ylab(,labs(vsmall)) 	///
	note("{bf:A)}", pos(10) ring(12) size(vsmall)) ///
	name(corr, replace) nodraw

*combine
gr combine corr iv_dist, ///
	col(2) ///
	name(corr_ivdist, replace)

gr export "figs/corr_ivdist.pdf", ///
	as(pdf) name("corr_ivdist") replace	

//Figure 2 - DV Distribution
foreach y of loc behav { 
	qui ologit `y' ///
		c.party_id##c.(expressive) neg_part ///
		`controls'
catplot `y' if e(sample), ///
	perc  asyvars vert ///
	ti("`: var label `y''") ///
	yti("Percent of Responses", s(small)) ///
	b1title("",) ///
	bar(1, bfcolor(maroon) lc(maroon)) ///
	bar(2, bfcolor(sand) lc(sand)) ///
	bar(3, bfcolor(gs12) lc(gs12)) ///
	bar(4, bfcolor(sienna) lc(sienna)) ///
	bar(5, bfcolor(edkblue) lc(edkblue)) ///
	yline(0, lc(black) lp(solid)) ///
	bargap(1) ///
	legend( ///
	ti("How much you would be willing to make the following changes to address climate change?", s(small)) ///
	pos(6) row(1) si(vsmall)) ///
	name(`y', replace) nodraw
}

grc1leg `behav', ///
	ti("{bf:Climate Behaviors}") ///
	row(1) ycomm ///
	name(dv_behav, replace)	
gr close dv_behav
	
foreach y of loc pol {
	qui ologit clim_pol_`y' ///
		c.party_id##c.(expressive) neg_part ///
		`controls'
catplot clim_pol_`y' if e(sample), ///
	perc  asyvars vert ///
	ti("`: var label clim_pol_`y''") ///
	yti("Percent of Responses", s(small)) ///
	b1title("",) ///
	bar(1, bfcolor(maroon) lc(maroon)) ///
	bar(2, bfcolor(sand) lc(sand)) ///
	bar(3, bfcolor(gs12) lc(gs12)) ///
	bar(4, bfcolor(sienna) lc(sienna)) ///
	bar(5, bfcolor(edkblue) lc(edkblue)) ///
	yline(0, lc(black) lp(solid)) ///
	bargap(1) ///
	legend( ///
	ti("How much you are in favor or against the following policies?", s(small)) ///
	pos(6) row(1) si(vsmall)) ///
	name(`y', replace) nodraw
}

grc1leg `pol', ///
	ti("{bf:Climate Policy}") ///
	row(1) ycomm ///
	name(dv_pol, replace)
gr close dv_pol
	
gr combine ///
	dv_pol ///
	dv_behav, col(1) ///
	name(dv_dist, replace)
	
gr export "figs/dv_dist.pdf", ///
	as(pdf) name("dv_dist") replace

// Figure 3 - Policy Support Interactions
loc fftax_pan A
loc renew_pan B
loc hhapp_pan C

foreach y of loc pol {
	qui sum clim_pol_`y', d
	loc `y'_max=r(max)
	qui ologit clim_pol_`y' ///
		party_id expressive neg_part ///
		`controls'
		qui r2o
		estadd scalar r2o=r(r2o)
		est sto olo_`y'
	
	qui ologit clim_pol_`y' ///
		c.party_id##c.(expressive) neg_part ///
		`controls'
			qui r2o
			estadd scalar r2o=r(r2o)
		est sto olo_`y'_Int_expressive
	qui ologit clim_pol_`y' ///
		c.party_id##c.(neg_part) ///
		expressive `controls'
			qui r2o
			estadd scalar r2o=r(r2o)
		est sto olo_`y'_Int_neg_part
	foreach x in expressive neg_part {
	est res olo_`y'_Int_`x'
	qui fre clim_pol_`y' if e(sample)
		loc valid_`y'=r(valid)[5,1]
		local tot_`y'=r(N)
		loc vperc_`y'=`valid_`y''/`tot_`y''
		di `vperc_`y''
	est res olo_`y'_Int_`x'
	margins, ///
		at(`x'=(-2(1)2) party_id=(1 7)) ///
		pr(out(``y'_max'))
	qui marginsplot, ///
			plot1opts(lc(maroon) m(th) mc(maroon)) ///
			plot2opts(lc(edkblue) m(dh) mc(edkblue)) ///
			ci1opts(lc(maroon%66)) ///
			ci2opts(lc(edkblue%66)) ///
			legend( ///
				order(3 "Republican" 4 "Democrat") ///
				pos(6) row(1)) ///
			yti("Pr('Strongly in favor')", s(small)) ///
			ylab(0(0.1)0.5, format(%3.2f) nogrid) ///
			yline(`vperc_`y'', lc(gs12)) ///
			xlab( ///
				-2 "-2Sd" -1 "-1Sd" 0 "Mean" 1 "+1Sd" 2 "+2Sd", nogrid) ///
			ti("") ///
			name(`y'_`x'_int, replace)	nodraw	
	}
grc1leg ///
	`y'_neg_part_int ///
	`y'_expressive_int, ///	
	row(1) ///
	note("{bf:``y'_pan')}", pos(10) ring(12) size(small)) ///
	ycomm name(`y'_int, replace) ///
	ti("`: var label clim_pol_`y''")
gr close `y'_int
gr close `y'_int
}

grc1leg ///
	fftax_int ///
	renew_int ///
	hhapp_int, ///
	col(1) ysize(10) ///
	name(pol_interaction, replace)
	
gr di, ysize(10) xsize(6.5)

gr export "figs/pol_interaction.pdf", ///
	as(pdf) name("pol_interaction") replace

// Figure 4 - Behaviors Interactions
loc offsets_pan A
loc commute_pan B
loc meat_pan C
loc bath_pan D


foreach y of loc behav {
	qui sum `y', d
	loc `y'_max=r(max)
	qui ologit `y' ///
		party_id expressive neg_part ///
		`controls'
		qui r2o
		estadd scalar r2o=r(r2o)
		est sto olo_`y'
	
	qui ologit `y' ///
		c.party_id##c.(expressive) neg_part ///
		`controls'
		qui r2o
		estadd scalar r2o=r(r2o)
		est sto olo_`y'_Int_expressive
	qui ologit `y' ///
		c.party_id##c.(neg_part) ///
		expressive `controls'
		qui r2o
		estadd scalar r2o=r(r2o)
		est sto olo_`y'_Int_neg_part
	foreach x in expressive neg_part {
	est res olo_`y'_Int_`x'
	qui fre `y' if e(sample)
		loc valid_`y'=r(valid)[5,1]
		local tot_`y'=r(N)
		loc vperc_`y'=`valid_`y''/`tot_`y''
		di `vperc_`y''
	est res olo_`y'_Int_`x'
	margins, ///
		at(`x'=(-2(1)2) party_id=(1 7)) ///
		pr(out(``y'_max'))
	marginsplot, ///
			plot1opts(lc(maroon) m(th) mc(maroon)) ///
			plot2opts(lc(edkblue) m(dh) mc(edkblue)) ///
			ci1opts(lc(maroon%66)) ///
			ci2opts(lc(edkblue%66)) ///
			legend( ///
				order(3 "Republican" 4 "Democrat") ///
				pos(6) row(1)) ///
			yti(`"Pr(Very Willing /"' `"I already do this)"', s(small)) ///
			ylab(0(0.2)0.6, format(%3.2f) nogrid gmax) ///
			yline(`vperc_`y'', lc(gs12)) ///
			xlab( ///
				-2 "-2Sd" -1 "-1Sd" 0 "Mean" 1 "+1Sd" 2 "+2Sd", nogrid) ///
			ti("") ///
			name(`y'_`x'_int, replace)	nodraw	
	}
grc1leg ///
	`y'_neg_part_int ///
	`y'_expressive_int, ///	
	row(1) ///
	note("{bf:``y'_pan')}", pos(10) ring(12) size(small)) ///
	ycomm name(`y'_int, replace) ///
	ti("`: var label `y''")
gr close `y'_int
gr close `y'_int
}

grc1leg ///
	offsets_int ///
	commute_int ///
	meat_int ///
	bath_int, ///
	col(1) ysize(10) ///
	name(behav_interaction, replace)
	
gr di, ysize(10) xsize(6.5)

gr export "figs/behav_interaction.pdf", ///
	as(pdf) name("behav_interaction") replace
	

//Supplementary Information - REMOVE /* and */ to run SI materials
/*
run "MayerSmithPartisanshipCC_SI.do"
*/	

